//
//  InviteFriendsViewController.m
//  PigMarket
//
//  Created by mac on 2019/3/26.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "InviteFriendsViewController.h"
#import <SGQRCodeObtain.h>

@interface InviteFriendsViewController ()
/**
 背景图
 */
@property (nonatomic , strong) UIImageView *bgImgView;

/**
 邀请码Lab
 */
@property (nonatomic , strong) UILabel *inviteCodeLab;

/**
 邀请码提示Lab
 */
@property (nonatomic , strong) UILabel *inviteCodeWarnLab;

/**
 邀请码
 */
@property (nonatomic , strong) UILabel *inviteCode;

/**
 邀请码点击复制
 */
@property (nonatomic , strong) UIButton *inviteCodeCopyBtn;

/**
 分享链接
 */
@property (nonatomic , strong) UILabel *shareLab;

/**
 点击分享复制
 */
@property (nonatomic , strong) UIButton *urlCopyBtn;

/**
 链接Lab
 */
@property (nonatomic , strong) UILabel *urlLab;

/**
 url
 */
@property (nonatomic , strong) UILabel *url;

/**
 扫一扫
 */
@property (nonatomic , strong) UILabel *richScanLab;

/**
 二维码
 */
@property (nonatomic , strong) UIImageView *qrCodeImgView;

/**
二维码边框
 */
@property (nonatomic , strong) UIImageView *qrCodeBorderImgView;

/**
 分享Btn
 */
@property (nonatomic , strong) UIButton *shareBtn;

/**
 分享链接边框
 */
@property (nonatomic , strong) UIView *shareUrlBorder;

/**
 复制
 */
@property (nonatomic , strong) UIPasteboard *pasteboard;
@end

@implementation InviteFriendsViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseNavView.navTitleLabel.text = @"邀请好友";
    [self setUpView];
}


#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.bgImgView];
    [self.bgImgView addSubview:self.inviteCodeLab];
    [self.bgImgView addSubview:self.inviteCodeWarnLab];
    [self.bgImgView addSubview:self.inviteCode];
    [self.bgImgView addSubview:self.inviteCodeCopyBtn];
    [self.bgImgView addSubview:self.shareLab];
    [self.bgImgView addSubview:self.urlCopyBtn];
    [self.bgImgView addSubview:self.shareUrlBorder];
    [self.shareUrlBorder addSubview:self.urlLab];
    [self.shareUrlBorder addSubview:self.url];
    [self.bgImgView addSubview:self.richScanLab];
    [self.bgImgView addSubview:self.qrCodeBorderImgView];
    [self.qrCodeBorderImgView addSubview:self.qrCodeImgView];
    [self.bgImgView addSubview:self.shareBtn];
}

- (void) initLayout {
    [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(TJ_NavBarHeight);
    }];

    [self.inviteCodeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(33);
        make.left.mas_equalTo(30);
        make.height.mas_equalTo(28);
    }];
    [self.inviteCodeWarnLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.inviteCodeLab.mas_right).offset(5);
        make.centerY.mas_equalTo(self.inviteCodeLab.mas_centerY);
        make.height.mas_equalTo(18);
    }];
    [self.inviteCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bgImgView.mas_centerX);
        make.top.mas_equalTo(self.inviteCodeWarnLab.mas_bottom).offset(10);
        make.height.mas_equalTo(26);
    }];
    [self.inviteCodeCopyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bgImgView.mas_centerX);
        make.top.mas_equalTo(self.inviteCode.mas_bottom).offset(2);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    [self.shareLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(150);
        make.left.mas_equalTo(self.inviteCodeLab.mas_left);
        make.height.mas_equalTo(26);
    }];
    [self.urlCopyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-34);
        make.centerY.mas_equalTo(self.shareLab.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    [self.shareUrlBorder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(192);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(48);
    }];
    [self.urlLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.shareUrlBorder.mas_centerY);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(36);
    }];
    [self.url mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.shareUrlBorder.mas_centerY);
        make.left.mas_equalTo(self.urlLab.mas_right).offset(2);
        make.right.mas_equalTo(self.shareUrlBorder.mas_right).offset(-10);
    }];
    [self.richScanLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bgImgView.mas_centerX);
        make.top.mas_equalTo(280);
        make.height.mas_equalTo(20);
    }];
    [self.qrCodeBorderImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.bgImgView.mas_centerX);
        make.top.mas_equalTo(self.richScanLab.mas_bottom).offset(18);
        make.size.mas_equalTo(CGSizeMake(180, 180));
    }];
    [self.qrCodeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.qrCodeBorderImgView.mas_centerX);
        make.centerY.mas_equalTo(self.qrCodeBorderImgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150, 150));
    }];
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.qrCodeBorderImgView.mas_bottom).offset(30);
        make.centerX.mas_equalTo(self.self.bgImgView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(170, 42));
    }];
}

- (void) initAction {
    
}

- (void)initData {
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    [[[PigMarketApi sharedInstance] inviteFriendsWithMemberId:memberId] subscribeNext:^(InviteFriends * _Nullable inviteFriends) {
        
        self.inviteCode.text = inviteFriends.memberNum;
        self.url.text = inviteFriends.url;
        self.qrCodeImgView.image = [SGQRCodeObtain generateQRCodeWithData:inviteFriends.url size:180];
    } error:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark - delegate

#pragma mark - target action
//复制邀请码
- (void)inviteCodeCopyTapped {
    self.pasteboard.string = self.inviteCode.text;
    PMLog(@"%@",self.pasteboard.string);
}

//复制url
- (void)urlCopyTapped {
    self.pasteboard.string = self.url.text;

}

//分享二维码
- (void)shareTapped {
    
}
#pragma mark - private method

#pragma mark - getter and setter
- (UIImageView *)bgImgView {
    if (!_bgImgView) {
        _bgImgView = [[UIImageView alloc] init];
        _bgImgView.image = [UIImage imageNamed:@"system_invitefriends_bg"];
        _bgImgView.userInteractionEnabled = YES;
    }
    return _bgImgView;;
}

- (UILabel *)inviteCodeLab {
    if (!_inviteCodeLab) {
        _inviteCodeLab = [[UILabel alloc] init];
        _inviteCodeLab.textColor = [UIColor textBlackColor];
        _inviteCodeLab.font = [UIFont xLargerFont];
        _inviteCodeLab.text = @"邀请码";
    }
    return _inviteCodeLab;
}
- (UILabel *)inviteCodeWarnLab {
    if (!_inviteCodeWarnLab) {
        _inviteCodeWarnLab = [[UILabel alloc] init];
        _inviteCodeWarnLab.textColor = [UIColor tipsGrayColor];
        _inviteCodeWarnLab.font = [UIFont smallFont];
        _inviteCodeWarnLab.text = @"使用您的邀请码邀请好友，可获得50元豚券";
    }
    return _inviteCodeWarnLab;
}
- (UILabel *)inviteCode {
    if (!_inviteCode) {
        _inviteCode = [[UILabel alloc] init];
        _inviteCode.textColor = [UIColor colorWithHexString:@"#F25C62"];
        _inviteCode.font = [UIFont xLargerFont];
    }
    return _inviteCode;
}
- (UIButton *)inviteCodeCopyBtn {
    if (!_inviteCodeCopyBtn) {
        _inviteCodeCopyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_inviteCodeCopyBtn setTitle:@"点击复制" forState:UIControlStateNormal];
        [_inviteCodeCopyBtn setTitleColor:[UIColor tipsGrayColor] forState:UIControlStateNormal];
        _inviteCodeCopyBtn.titleLabel.font = [UIFont smallFont];
        [_inviteCodeCopyBtn addTarget:self action:@selector(inviteCodeCopyTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _inviteCodeCopyBtn;
}
- (UILabel *)shareLab {
    if (!_shareLab) {
        _shareLab = [[UILabel alloc] init];
        _shareLab.textColor = [UIColor textBlackColor];
        _shareLab.font = [UIFont xLargerFont];
        _shareLab.text = @"分享链接";
    }
    return _shareLab;
}
- (UIButton *)urlCopyBtn {
    if (!_urlCopyBtn) {
        _urlCopyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_urlCopyBtn setTitle:@"点击复制" forState:UIControlStateNormal];
        [_urlCopyBtn setTitleColor:[UIColor tipsGrayColor] forState:UIControlStateNormal];
        _urlCopyBtn.titleLabel.font = [UIFont middleFont];
        [_urlCopyBtn addTarget:self action:@selector(urlCopyTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _urlCopyBtn;
}
- (UIView *)shareUrlBorder {
    if (!_shareUrlBorder) {
        _shareUrlBorder = [[UIView alloc] init];
        _shareUrlBorder.layer.borderColor = [UIColor colorWithHexString:@"#EAEAEA"].CGColor;
        _shareUrlBorder.layer.borderWidth = 1;
        _shareUrlBorder.layer.cornerRadius = 3;
        _shareUrlBorder.layer.masksToBounds = YES;
    }
    return _shareUrlBorder;
}
- (UILabel *)urlLab {
    if (!_urlLab) {
        _urlLab = [[UILabel alloc] init];
        _urlLab.textColor = [UIColor tipsGrayColor];
        _urlLab.font = [UIFont smallFont];
        _urlLab.text = @"链接:";
    }
    return _urlLab;
}
- (UILabel *)url {
    if (!_url) {
        _url = [[UILabel alloc] init];
        _url.textColor = [UIColor textBlackColor];
        _url.font = [UIFont largerFont];
    }
    return _url;
}
- (UILabel *)richScanLab {
    if (!_richScanLab) {
        _richScanLab = [[UILabel alloc] init];
        _richScanLab.textColor = [UIColor colorWithHexString:@"#666666"];
        _richScanLab.font = [UIFont middleFont];
        _richScanLab.text = @"扫一扫，码上找到你";
    }
    return _richScanLab;
}
- (UIImageView *)qrCodeBorderImgView {
    if (!_qrCodeBorderImgView) {
        _qrCodeBorderImgView = [[UIImageView alloc] init];
        _qrCodeBorderImgView.image = [UIImage imageNamed:@"system_invitefriend_qrcode"];
    }
    return _qrCodeBorderImgView;
}
- (UIImageView *)qrCodeImgView {
    if (!_qrCodeImgView) {
        _qrCodeImgView  = [[UIImageView alloc] init];
        
    }
    return _qrCodeImgView;
}
- (UIButton *)shareBtn {
    if (!_shareBtn) {
        _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shareBtn setBackgroundImage:[UIImage imageNamed:@"system_invitefriend_share"] forState:UIControlStateNormal];
        [_shareBtn addTarget:self action:@selector(shareTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shareBtn;
}
- (UIPasteboard *)pasteboard {
    if (!_pasteboard) {
        _pasteboard = [UIPasteboard generalPasteboard];
    }
    return _pasteboard;
}
@end
